package com.qianxiwitdoctor.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianxi.wit.pojo.TbDiagnosticLogout;
import com.qianxi.wit.pojo.TbDoctor;
import com.qianxi.wit.pojo.TbFaculty;
import com.qianxi.wit.pojo.TbFacultyDetails;
import com.qianxi.wit.result.R;
import com.qianxi.wit.result.Result;
import com.qianxiwitdoctor.mapper.TbDoctorMapper;
import com.qianxiwitdoctor.mapper.TbFacultyDetailsMapper;
import com.qianxiwitdoctor.mapper.TbFacultyMapper;
import com.qianxiwitdoctor.service.TbSuffererService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 * 医生表 前端控制器
 * </p>
 *
 * @author wyh
 * @since 2021-01-09
 */
@RestController
@RequestMapping
public class TbDoctorController {
        @Autowired
        private TbDoctorMapper doctorMapper;
        @Autowired
        private TbFacultyMapper facultyMapper;
        @Autowired
        private TbFacultyDetailsMapper facultyDetailsMapper;
        @Autowired
        private TbSuffererService tbSuffererService;

        //根据医院id查询
        @PostMapping("/faculty/register/{hospitalId}/{pageNum}/{pageSize}")
        public R Flist(@PathVariable("hospitalId") long hospitalId,
                       @PathVariable("pageNum") int pageNum,
                       @PathVariable("pageSize") int pageSize,
                       @RequestBody TbFaculty faculty){

            try {
                Page<TbFaculty> page = new Page<>(pageNum,pageSize);
                QueryWrapper<TbFaculty> wrapper = new QueryWrapper<>();
                wrapper.eq("hostipital_id",hospitalId);
                if(!"".equals(faculty.getFacultyName())){
                    wrapper.like("faculty_name",faculty.getFacultyName());
                }
                facultyMapper.selectPage(page,wrapper);
                return R.ok().data("total",page.getTotal()).data("list",page.getRecords());
            }catch (Exception e){
                return R.error();
            }
        }

    //科室详情
    @GetMapping("/details/statistics/{facultyId}")
    public List<TbFacultyDetails> statistics(@PathVariable("facultyId")BigInteger facultyId){
        LambdaQueryWrapper<TbFacultyDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TbFacultyDetails::getFacultyId,facultyId);
        return facultyDetailsMapper.selectList(wrapper);
    }




    @PostMapping("getList/doctor/{pageNum}/{pageSize}")
    public IPage<TbDoctor> getList(@RequestBody TbDoctor doctor,
                                   @PathVariable("pageNum")int pageNum,
                                   @PathVariable("pageSize")int pageSize
    ){
        LambdaQueryWrapper<TbDoctor> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(doctor.getDoctorName()),TbDoctor::getDoctorName,doctor.getDoctorName());
        return  doctorMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);



    }


        //医生诊断的详情
    @PostMapping("/diagnostic/findAll/{doctorId}/{pageNum}/{pageSize}")
    private R findAll(@PathVariable("doctorId") long doctorId,
                      @PathVariable("pageNum") long pageNum,
                      @PathVariable("pageSize") long pageSize,
                      @RequestBody TbDoctor doctorName){
        try {
            Page<TbDiagnosticLogout> list =  tbSuffererService.findSufferer(doctorId,doctorName.getDoctorName(),pageNum,pageSize);

            return R.ok().data("total",list.getTotal()).data("list",list.getRecords());
        }catch (Exception e){
            return R.error();
        }
    }
}

